﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Web;

namespace STAR.Spring.Framework.Module
{
    public delegate dynamic CustomExecute(DataTable dt);

    public class ExecuteService
    {
        event CustomExecute CustomExecuteEvent;

        public ExecuteService()
        {
            if (string.IsNullOrEmpty(DBProcess.dbHelper.ConnectionString))
            {
                DBProcess.dbHelper.ConnectionString = System.Configuration.ConfigurationManager.AppSettings["ConnectionString"];
            }
        }

        public ExecuteService(string connectionString)
        {
            DBProcess.dbHelper.ConnectionString = connectionString;
            if (string.IsNullOrEmpty(DBProcess.dbHelper.ConnectionString))
            {
                DBProcess.dbHelper.ConnectionString = System.Configuration.ConfigurationManager.AppSettings["ConnectionString"];
            }
        }

        public ReturnInfo GetValueRequest(bool isCustom, RequestJsonParmInfo info)
        {
            return CommonRequest(ReturnDataType.单值, isCustom, info.RequestName, info.Parms);
        }
        public ReturnInfo GetObjectRequest(bool isCustom, RequestJsonParmInfo info)
        {
            return CommonRequest(ReturnDataType.对象, isCustom, info.RequestName, info.Parms);
        }
        public ReturnInfo GetListRequest(bool isCustom, RequestJsonParmInfo info)
        {
            return CommonRequest(ReturnDataType.列表, isCustom, info.RequestName, info.Parms);
        }
        //public ReturnInfo GetCustomRequest(bool isCustom, RequestJsonParmInfo info)
        //{
        //    return CommonRequest(ReturnDataType.部分自定义, isCustom, info.RequestName, info.Parms);
        //}
        public ReturnInfo GetFullCustomRequest(RequestJsonParmInfo info)
        {
            return CommonRequest(ReturnDataType.自定义, true, info.RequestName, info.Parms);
        }

        private ReturnInfo CommonRequest(ReturnDataType requestType, bool isCustom, string requestName, params object[] parms)
        {
            ReturnInfo response = new ReturnInfo();
            try
            {
                ICustomResponse customResponse = null;
                if (isCustom || requestType == ReturnDataType.自定义)
                {
                    customResponse = CustomResponseFactory.GetCustomResponse(requestName);
                }
                else
                {
                    customResponse = new CustomResponse();
                }
                customResponse.requestType = requestType;
                ///数据
                DataTable dt = null;
                if (requestType != ReturnDataType.自定义)
                {
                    if (customResponse.BeforeReadData(requestName, parms))
                    {
                        dt = customResponse.ReadData(requestName, parms);
                    }
                    customResponse.AfterReadData(dt);
                    //response.MainData = customResponse.CustomProcess(parms);

                    if (customResponse.BeforeCreateResult(dt))
                    {
                        response.MainData = customResponse.CreateResult(dt);
                    }
                    customResponse.AfterCreateResult(response.MainData);
                    if (HttpContext.Current.Request["paged"] == "1" && response.MainData is List<dynamic>)
                    {
                        response.MainData = ((List<dynamic>)response.MainData).GetPagedData<dynamic>();
                    }
                }
                else
                {
                    response.MainData = customResponse.CustomProcess(requestName, parms);
                }
                response.returnValue = true;
            }
            catch (Exception ex)
            {
                //log记录真正的异常
                Log.LogWorker.logger.DebugFormat("Spring执行异常：{0}\r\n{1}", ex.Message, ex.StackTrace);
                response.message = "调用数据错误";// ex.Message;
                response.returnValue = false;
            }
            return response;
        }
    }
}
